package nus.iss.ga.ui;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;

public class DataFileTableModel extends AbstractTableModel {
    
    //protected Vector data;
    protected Vector<String> data;
    protected Vector columnNames ;
    protected String datafile;
    
    public DataFileTableModel(String f){
        datafile = f;
        initVectors();
    }
    
    public DataFileTableModel(Vector columns, Vector<String> results){
    	this.columnNames = columns;
    	this.data = results;
    }
    
    public void initVectors() {
        
        String aLine ;
        //data = new Vector();
        data = new Vector<String>();

        columnNames = new Vector();
        
        try {
            FileInputStream fin =  new FileInputStream(datafile);
            BufferedReader br = new BufferedReader(new InputStreamReader(fin));
            // extract column names
            StringTokenizer st1 =
                    new StringTokenizer(br.readLine(), "|");
            while(st1.hasMoreTokens())
                columnNames.addElement(st1.nextToken());
            // extract data
            while ((aLine = br.readLine()) != null) {
                StringTokenizer st2 =
                        new StringTokenizer(aLine, "|");
                while(st2.hasMoreTokens())
                    data.addElement(st2.nextToken());
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public int getRowCount() {
        return data.size() / getColumnCount();
    }
    
    public int getColumnCount(){
        return columnNames.size();
    }
    
    public String getColumnName(int columnIndex) {
        String colName = "";
        
        if (columnIndex <= getColumnCount())
            colName = (String)columnNames.elementAt(columnIndex);
        
        return colName;
    }
    
    public Class getColumnClass(int columnIndex){
        return String.class;
    }
    
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }
    
    public Object getValueAt(int rowIndex, int columnIndex) {
        return (String)data.elementAt( (rowIndex * getColumnCount()) + columnIndex);
    }
    
    /*
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        return;
    }*/
    
    
    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    	System.out.println("Set method is fired");
    	String value = (String)data.elementAt( (rowIndex * getColumnCount()) + columnIndex);
    	value = (String)aValue;
    	Object val = data.get( (rowIndex * getColumnCount()) + columnIndex);
    	val = aValue;
    	data.setElementAt(value, (rowIndex * getColumnCount()) + columnIndex);
    	System.out.println("val =" + val);
    	System.out.println("row index =" + rowIndex);
    	System.out.println("column count " + getColumnCount());
    	System.out.println("columnIndex " + columnIndex);
    	System.out.println("Data size " + data.size());
    	System.out.println("Data Value originally " + (String)data.elementAt( (rowIndex * getColumnCount()) + columnIndex));
    	//data.add((rowIndex * getColumnCount()), value);
    	System.out.println("Data Value after " + (String)data.elementAt( (rowIndex * getColumnCount()) + columnIndex));
    	System.out.println("Data size " + data.size());
    	this.fireTableCellUpdated(rowIndex, columnIndex);
    }
    
 

}
